<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FhKnow;
use App\Models\FhCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FhKnowController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FhKnow(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id','desc');
            $grid->column('title');
            $grid->column('url');
            $grid->column('desc')->width(400);
            $grid->column('keyword');
            $grid->column('pid')->display(function ($p_id) {
                $title = FhCategory::where('id', $p_id)->value('c_name');
                return $title;
            });;
            $grid->column('show')->switch();
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FhKnow(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('url');
            $show->field('desc');
            $show->field('keyword');
            $show->field('pid');
            $show->field('show');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FhKnow(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();;
            $form->text('url')->required()->help("例如 /test");
            $form->text('desc');
            $form->text('keyword');
            $form->select('pid')->options('/apis/top')->default(1)->required();
            $form->switch('show')->default(1);
            $form->editor('content');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
